package lock.sales;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @AUTHOR LYF
 * @DATE 2022/3/19
 * @VERSION 1.0
 * @DESC
 */

class PrintResource{
      int flag;
      ReentrantLock lock = new ReentrantLock();
      Condition condition1 = lock.newCondition();
      Condition condition2 = lock.newCondition();
      Condition condition3 = lock.newCondition();

      void print5(int flag,int loop){//1--指令命令打印该
          lock.lock();//上锁
          try {
              while(flag!=1)
                condition1.await();

              System.out.println(Thread.currentThread().getName()+"::进行第"+loop+"轮打印");
              for (int i = 0; i < 5; i++) {
                  System.out.println(Thread.currentThread().getName()+"::打印"+i);
              }

              flag = 2;//通知2
              condition2.signalAll();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }finally {
              lock.unlock();
          }

      }
      void print10(int flag,int loop){
          lock.lock();//上锁
          try {
              while(flag!=2)
                  condition2.await();

              System.out.println(Thread.currentThread().getName()+"::进行第"+loop+"轮打印");
              for (int i = 0; i < 10; i++) {
                  System.out.println(Thread.currentThread().getName()+"::打印"+i);
              }

              flag = 3;//通知2
              condition3.signalAll();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }finally {
              lock.unlock();
          }

      }
    void print15(int flag,int loop){
        lock.lock();//上锁
        try {
            while(flag!=3)
                condition3.await();

            System.out.println(Thread.currentThread().getName()+"::进行第"+loop+"轮打印");
            for (int i = 0; i < 15; i++) {
                System.out.println(Thread.currentThread().getName()+"::打印"+i);
            }

            flag = 1;//通知2
            condition1.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

    }


}

public class CommunicationDemo {
    public static void main(String[] args) {
        PrintResource
                printResource = new PrintResource();
        for(int i=0;i<3;i++){

            new Thread(()->{
                int loop =Integer.valueOf(Thread.currentThread().getName().substring(7));
                printResource.print5(1,loop);
            },"Thread-1").start();

            new Thread(()->{
                int loop =Integer.valueOf(Thread.currentThread().getName().substring(7));
                printResource.print10(2,loop);
            },"Thread-2").start();

            new Thread(()->{
                int loop =Integer.valueOf(Thread.currentThread().getName().substring(7));
                printResource.print15(3,loop);
            },"Thread-3").start();

        }
    }
}
